﻿using LJX.SmartPark.Common.Extensions;
using LJX.SmartPark.Common.ResponseOutput;
using LJX.SmartPark.Service.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LJX.SmartPark.Domain.SysTools;
namespace LJX.SmartPark.Service.SysTools
{
    public class FormConstructionService:IFormConstructionService
    {
        private readonly IRepository<FormConstruction> _FormConstructionService;

        public FormConstructionService(IRepository<FormConstruction> FormConstructionService)
        {
            _FormConstructionService = FormConstructionService;
        }

        FormConstruction FormConstructions = new FormConstruction();
        //智能灯列表显示
        public async Task<Response<List<FormConstruction>>> GetFormConstructionAsync()
        {
            var FormConstruction = await _FormConstructionService.GetAllListAsync();
            var result = FormConstruction.MapToList<FormConstruction>();
            return Response<List<FormConstruction>>.Ok(result);
        }
        //智能灯列表显示根据设备id查询
        public Response<FormConstruction> GetFormConstructionData(FormConstruction input)
        {
            var FormConstruction = _FormConstructionService.GetListQuery().FirstOrDefault(m => m.Id == input.Id);
            var result = FormConstruction.MapTo<FormConstruction>();
            return Response<FormConstruction>.Ok(result);
        }
        //智能灯添加
        public async Task<Response<bool>> FormConstructionAdd(FormConstruction input)
        {
            var result = _FormConstructionService.InsertAsyncById(input.MapTo<FormConstruction>());

            return Response<bool>.Ok(result.Result > 0);
        }
        //智能灯删除
        public async Task<Response<bool>> FormConstructionDelete(FormConstruction input)
        {
            var result = await _FormConstructionService.DeleteByIdAsync(input.MapTo<FormConstruction>());
            return Response<bool>.Ok(result > 0);
        }

        //智能灯修改
        public async Task<Response<bool>> FormConstructionUpdate(FormConstruction input)
        {
            var result = _FormConstructionService.UpdateAsync(input.MapTo<FormConstruction>());

            return Response<bool>.Ok(result.Result > 0);
        }
        //智能灯批量删除
        public async Task<Response<bool>> FormConstructionDeleteDatas(string[] ids)
        {

            var dtos = _FormConstructionService.GetListQuery().Where(m => ids.Contains(m.Id.ToString()));
            var result = await _FormConstructionService.DeleteAsync(dtos.ToArray());
            return Response<bool>.Ok(result > 0);
        }
        //总条数
        public int GetCount()
        {
            return _FormConstructionService.GetCount();
        }


        //显示分页查询
        public async Task<Response<List<FormConstruction>>> GetFormConstructionPage(SelectLogData select)
        {
            var datas = await _FormConstructionService.GetAllListAsync();
            var result = datas.MapToList<FormConstruction>();

            if (!string.IsNullOrWhiteSpace(select.FormName))
            {
                result = result.Where(x => x.FormName.Contains(select.FormName)).ToList();
            }
            var totalCount = result.Count();

            result = result.Skip(((select.pageIndex - 1) * select.pageSize)).Take(select.pageSize).ToList();

            return Response<List<FormConstruction>>.Ok(result, totalCount);
        }

        //页数条数model类
        public class SelectLogData
        {
            public int pageIndex { get; set; }
            public int pageSize { get; set; }
            public string FormName { get; set; }
        }
    }
}
